Creating gifs of satellite images

The script downloads images from the CoastWatch ERDDAP server and then creates a gif using ImageMagick.

You will need some packages for this. Here are install instructions for those on GitHub.

require(devtools)
devtools::install_github("ropensci/rerddap")
devtools::install_github("rmendels/rerddapXtracto") 

Load the packages.

library(rerddap)
library(rerddapXtracto)
library(ggplot2) # plotting
library(dplyr) # for %>% pipe
library(purrr) # for map()
library(magick) # for image_* functions

Step 1. Install ImageMagick if needed

Here is how to do it on a Mac (MacOS Sierra 10.12.6); Google to figure this out for Windows or Unix.

  1. Open up utilities (in apps), and open Terminal.
  2. Type the following on the command line to install brew

    ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
  3. Then you can install ImageMagick with this command.

    brew install imagemagick

Step 2. Create a folder for the downloaded images

fil_dir <- paste0("india_sst_pngs_",year)
if(!dir.exists(fil_dir)) dir.create(fil_dir)

Step 3. Download pngs from CoastWatch

We will download SST images from the Global SST & Sea Ice Analysis, L4 OSTIA, UK Met Office, Global, 0.05°, Daily, 2013-present product. Here is the data access page for that dataset.

We will create a url for each day that we want to download. The url will look like

https://coastwatch.pfeg.noaa.gov/erddap/griddap/jplUKMO_OSTIAv20.png?analysed_sst%5B(2014-12-31T12:00:00Z)%5D%5B(7.125):(15.125)%5D%5B(72.625):(78.375)%5D&.draw=surface&.vars=longitude%7Clatitude%7Canalysed_sst&.colorBar=%7C%7C%7C24%7C34%7C&.bgColor=0xffccccff&.trim=0&.size=300

We want to keep everything except the dates. We will update the date for each image.

url1="https://coastwatch.pfeg.noaa.gov/erddap/griddap/jplUKMO_OSTIAv20.png?analysed_sst%5B("
url2="T12:00:00Z)%5D%5B("
url3="):("
url4=")%5D%5B("
url5="):("
url6=")%5D&.draw=surface&.vars=longitude%7Clatitude%7Canalysed_sst&.colorBar=%7C%7C%7C24%7C34%7C&.bgColor=0xffccccff&.trim=0&.size="
size=300
lon1 <- 72.625; lon2 <- 78.375
lat1 <- 7.125; lat2 <- 15.125
}

Now we go through each month in a year and download the pngs for that satellite image.

year="2015"
for(mon in 1:12){
  for(i in seq(1,31,2)){ # i is day
    # day needs to be like 01 instead of 1
    day=formatC(i, width = 2, format = "d", flag = "0")
    month=formatC(mon, width = 2, format = "d", flag = "0")
    # put the url together
    url=paste0(url1, year, "-", month, "-", day, url2,lat1,url3,lat2,url4,
               lon1,url5,lon2,url6,size)
    # make the filename
    fil=paste0(fil_dir,"/file-",year,"-",month,"-",day,".png")
    # wrap in try() so doesn't crash if no file for that day
    try(download.file(url,destfile=fil, mode="wb"))
  }
}

Step 4. Add a header to our Gifs with the year, month and day

I am adding an annotation to the top. I could also create an image header and append that to the top.

library(stringr)
files = list.files(path = fil_dir, pattern = "*.png", full.names = T)
for(i in files){
  yr=str_split(i,"-")[[1]][2]
  mon=month.abb[as.numeric(str_split(i,"-")[[1]][3])]
  day=as.numeric(str_split(str_split(i,"-")[[1]][4],"[.]")[[1]][1])
  ann.text = paste(yr,mon,day)
  img = image_read(i)
  img = image_annotate(img, ann.text, size = 20, color = "black", location = "+130+0")
  image_write(img, i, 'png')  
}

Step 5. Make animation

gif_fil <- paste0("kochin_sst_", year, ".gif")
list.files(path = fil_dir, pattern = "*.png", full.names = T) %>% 
  map(image_read) %>% # reads each path file
  image_join() %>% # joins image
  image_animate(fps=4, loop=1) %>% # animates, can opt for number of loops
  image_write(gif_fil) # write to current dir

Finished Product

Gif of SST off SW Coast of India Gif of SST off SW Coast of India